//
//  LLColletcionSlider.swift
//  LaiLang
//
//  Created by David on 2017/2/4.
//  Copyright © 2017年 David. All rights reserved.
//

import UIKit

class LLCollectionSlider: UIView, UIScrollViewDelegate {
    var scrollView: UIScrollView!
    
    private var _views: [UIView]?
    
    private var pageView: UIPageControl!
    
//    private var pauseAutoPage = false
//    
//    var autoPageDuration = 5
//    private var autoPageCounting = 0
//    
//    private var _cycleEnable = true
//    var cycleEnable: Bool{
//        get{
//            return _cycleEnable
//        }
//        set(v){
//            scrollView.alwaysBounceHorizontal = !v
//            _cycleEnable = v
//        }
//    }
    
    var autoPageEnable = true
    
    var views: [UIView]? {
        get{
            return _views
        }
        set(v){
            if _views != nil {
                for iv in _views!{
                    iv.removeFromSuperview()
                }
            }
            if v != nil {
                scrollView.contentSize = CGSize(width: frame.width * CGFloat(v!.count), height: frame.height)
                var ivs: [UIView] = []
                for i in 0..<v!.count{
                    let view = v![i]
                    view.frame = CGRect(x: frame.width * CGFloat(i), y: 0, width: frame.width, height: frame.height)
                    scrollView.addSubview(view)
                    ivs.append(view)
                    
                }
                
                pageView.numberOfPages = v!.count
                
                
                if ivs.count != 0{
                    _views = ivs
                }else{
                    _views = nil
                }
                
            }
            
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        scrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: frame.width, height: frame.height))
        scrollView.isPagingEnabled = true
        scrollView.alwaysBounceHorizontal = true
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.delegate = self
        addSubview(scrollView)
        
        pageView = UIPageControl(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
        pageView.center.x = scrollView.center.x
        pageView.center.y = frame.height
        pageView.pageIndicatorTintColor = UIColor.lightGray
        pageView.currentPageIndicatorTintColor = UIColor.black
        addSubview(pageView)
        
//        Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(LLSlider.autoPage), userInfo: nil, repeats: true)
    }
    

    internal func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        pageView.currentPage = Int(scrollView.contentOffset.x / frame.width)
    }
    
//    internal func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
//        pauseAutoPage = true
//    }
//    
//    internal func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
//        autoPageCounting = 0
//        pauseAutoPage = false
//    }
    
//    func autoPage(){
//        if autoPageEnable{
//            if !pauseAutoPage {
//                if _imageViews != nil {
//                    if _imageViews!.count != 0 {
//                        autoPageCounting = (autoPageCounting + 1) % autoPageDuration
//                        if autoPageCounting == 0{
//                            scrollView.setContentOffset(CGPoint(x: frame.width + CGFloat(pageView.currentPage + 1) * frame.width, y: 0), animated: true)
//                        }
//                        
//                        
//                    }
//                }
//                
//            }
//        }
//        
//    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
